package com.xx.pmis.persistence.dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.xx.pmis.model.Diary;

public class DiaryDAOImpl extends BaseHibernateDAOImpl<Diary> implements DiaryDAO {

	public Diary getDiaryByDiaryID(String diaryID) {
		Diary diary;
		try {
			diary = (Diary) getSession().get(Diary.class, diaryID);

		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return diary;
	}

	@SuppressWarnings("unchecked")
	public List<Diary> getAllDiary() {
		List<Diary> diarys;
		try {
			Query query = getSession().getNamedQuery("AllDiaryQuery");
			diarys = query.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return diarys;
	}

	@SuppressWarnings("unchecked")
	public List<Diary> getAllDiaryByPage(int currentPage, int lineSize) {
		List<Diary> diarys;
		try {
			Query query = getSession().getNamedQuery("AllDiaryByPageQuery");
			query.setFirstResult((currentPage - 1) * lineSize);
			query.setMaxResults(lineSize);
			diarys = query.list();
		} catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		return diarys;
	}

}
